package com.hw.paltfrom.service.casemain;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.exception.HWSystemException;
import com.hw.paltfrom.mapper.HwBaseCountycoreMapper;
import com.hw.paltfrom.mapper.HwCaseMainMapper;
import com.hw.paltfrom.mapper.HwSupervisionMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.*;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.service.doncutor.BaseWorkflowService;
import com.hw.paltfrom.service.doncutor.CommandWorkflowService;
import com.hw.paltfrom.utils.PageUtils;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.CaseMainCityVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;


/**
 * 执法监管(市级)
 * 梁天
 * 2019年11月7日
 */
@Service
public class CityCasemainServer {

    @Autowired
    HwCaseMainMapper hwCaseMainMapper;
    @Autowired
    HwSupervisionMapper hwSupervisionMapper;
    @Autowired
    private BaseWorkflowService baseWorkflowService;
    @Autowired
    private CommandWorkflowService commandWorkflowService;
    @Autowired
    private HwBaseCountycoreMapper hwBaseCountycoreMapper;

    //列表
    public Map<String, Object> query(CaseMainCityVO vo) {

        // 分页
        Integer pageNum = vo.getPageNum();
        Integer pageSize = vo.getPageSize();
        PageHelper.startPage(pageNum, pageSize);

        //区域编码和站名和站点编码
        String areaCode = vo.getfAreacode();
        String overrunName = vo.getfOverrunname();
        String overrunCode = vo.getfOverruncode();


        //阶段
        String crtStep = vo.getfAjCrtstep();
        //车牌号
        String cph = vo.getfClTruckno();
        //超重
        Double cz = vo.getfClOverweight();
        String symbol = vo.getSymbol();

        // 静态称重
        Double jcz = vo.getfClTotalweight();
        String symbol2 = vo.getSymbol2();

        // 轴数
        String axis = vo.getfClAxis();
        String symbol3 = vo.getSymbol3();

        // 检测开始时间
        Timestamp StartTime = null;
        // 检测结束时间
        Timestamp EndTime = null;

        if (vo.getStartTime() == null) {
            if (vo.getEndTime() != null) {
                return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
            }
        } else {
            if (vo.getEndTime() != null) {
                StartTime = Timestamp.valueOf(vo.getStartTime());
                EndTime = Timestamp.valueOf(vo.getEndTime());
            } else {
                return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
            }
        }


        // 责任倒查
        Integer investigate = vo.getfAjInvestigate();
        // 交警处罚
        Integer police = vo.getfAjPolice();
        // 放行类型
        Integer goState = vo.getfAjGostate();

        Wrapper<HwCaseMain> casewrapper = new EntityWrapper<>();


        if(StringUtils.isNotBlank(areaCode)){
            casewrapper.eq("F_AreaCode", areaCode);
        }
        if(StringUtils.isNotBlank(overrunName)){
            casewrapper.eq("F_OverrunName", overrunName);
        }
        if(StringUtils.isNotBlank(overrunCode)){
            casewrapper.eq("F_OverrunCode", overrunCode);
        }


        if(StringUtils.isNotBlank(crtStep)){
            casewrapper.eq("F_AJ_CrtStep", crtStep);
        }else {
            casewrapper.ne("F_AJ_CrtStep", "07");
        }

        if (StringUtils.isNotBlank(cph)) {
            casewrapper.like("F_CL_TruckNo", cph);
        }
        if (StringUtils.isNotBlank(symbol) && cz != null) {
            switch (symbol) {
                case "0":
                    casewrapper.ge("F_CL_OverWeight", cz);
                    break;
                case "1":
                    casewrapper.eq("F_CL_OverWeight", cz);
                    break;
                case "2":
                    casewrapper.le("F_CL_OverWeight", cz);
                    break;
            }
        }
        if (jcz != null && StringUtils.isNotBlank(symbol2)) {
            switch (symbol2) {
                case "0":
                    casewrapper.ge("F_CL_TotalWeight", jcz);
                    break;
                case "1":
                    casewrapper.eq("F_CL_TotalWeight", jcz);
                    break;
                case "2":
                    casewrapper.le("F_CL_TotalWeight", jcz);
                    break;
            }
        }

        if (StartTime != null && EndTime != null) {
            casewrapper.between("F_CL_CheckTime", StartTime, EndTime);
        }

        if (StringUtils.isNotBlank(axis) && StringUtils.isNotBlank(symbol3)) {
            switch (symbol3) {
                case "0":
                    casewrapper.ge("F_CL_Axis", axis);
                    break;
                case "1":
                    casewrapper.eq("F_CL_Axis", axis);
                    break;
                case "2":
                    casewrapper.le("F_CL_Axis", axis);
                    break;
            }
        }
        if (investigate != null) {
            casewrapper.eq("F_AJ_Investigate", investigate);
        }
        if (police != null) {
            casewrapper.eq("F_AJ_Police", police);
        }
        if (goState != null) {
            casewrapper.eq("F_AJ_GoState", goState);
        }

        casewrapper.orderBy("F_CL_CheckTime");
        List<HwCaseMain> hwCaseMainList = hwCaseMainMapper.selectList(casewrapper);
        PageInfo<HwCaseMain> info = new PageInfo<>(hwCaseMainList);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
    }


    //督导
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class,
            HWSystemException.class, })
    public Map<String, Object> superVision(CaseMainCityVO vo) {

        // 创建基础工作流
        HwBaseWorkflow hwBaseWorkflow;
        // 查询下一步的工作流code
        hwBaseWorkflow = baseWorkflowService.getStartBaseWorkflow("JCDD");
        String nowFlowStage = hwBaseWorkflow.getWorkflowCode();
        String nowFlowStageName=hwBaseWorkflow.getWorkflowCodename();
        String nextFlowStage = hwBaseWorkflow.getWorkflowNextcode();
        String nextFlowStageName = hwBaseWorkflow.getWorkflowNextcodename();

        String spvId = RandomNumberUtil.getNumber();
        HwSupervision hwSupervision = new HwSupervision();
        hwSupervision.setfSpvid(spvId);
        hwSupervision.setfFromobjectid(vo.getfCaseid());
        hwSupervision.setfTitle(vo.getfTitle());
        hwSupervision.setfFromtype(vo.getfFromusername());
        hwSupervision.setfFromdepcode(vo.getfFromdepcode());
        hwSupervision.setfFromuserid(vo.getfFromuserid());
        hwSupervision.setfFromusername(vo.getfFromusername());
        hwSupervision.setfFromtime(new Timestamp(System.currentTimeMillis()));
        hwSupervision.setfFromsrc(2);
        hwSupervision.setfSpvcontent(vo.getfSpvcontent());
        hwSupervision.setfBacktype(vo.getfBackdeptype());
        hwSupervision.setfBackdepcode(vo.getfBackdepcode());
        hwSupervision.setfBackdepname(vo.getfBackdepname());
        hwSupervision.setfCurrentstep(nextFlowStage);
        hwSupervision.setIsStagestate(0);
        hwSupervision.setfBackassignstate(0);
        Integer flag = hwSupervisionMapper.insert(hwSupervision);
        if (flag != 1) {
            throw new HWSystemException();
        }
        Wrapper<HwCaseMain> wrapper = new EntityWrapper<>();
        wrapper.eq("F_CaseId", vo.getfCaseid());
        HwCaseMain hwCaseMain = new HwCaseMain();
        HwCaseMain hwCaseMain1 = hwCaseMainMapper.selectById(vo.getfCaseid());
        String flowStage = hwCaseMain1.getfAjCrtstep();
        String flowCode = hwCaseMain1.getFlowCode();
        // 更新新工作流code
        hwCaseMain.setfAjCrtstep(nowFlowStage);
        Integer update = hwCaseMainMapper.update(hwCaseMain, wrapper);
        if (update != 1) {
            throw new HWSystemException();
        }

        // 创建各阶段审批操作过程
        HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
        hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());
        hwCommandWorkflow.setWorkflowModule(hwBaseWorkflow.getWorkflowModule());
        hwCommandWorkflow.setWorkflowType(hwBaseWorkflow.getWorkflowType());
        hwCommandWorkflow.setWorkflowCode(nowFlowStage);
        hwCommandWorkflow.setWorkflowCodename(hwBaseWorkflow.getWorkflowCodename());
        hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
        hwCommandWorkflow.setfAccount(vo.getfAccount());
        hwCommandWorkflow.setfFromdepcode(vo.getfFromdepcode());
        hwCommandWorkflow.setfFromdepname(vo.getfFromtype());
        hwCommandWorkflow.setCommandworkflowDatatime(new Date());
        hwCommandWorkflow.setObjectId(spvId);
        hwCommandWorkflow.setWorkflowNextcode(nextFlowStage);
        hwCommandWorkflow.setWorkflowNextcodename(nextFlowStageName);
        hwCommandWorkflow.setCommandworkflowContent(vo.getfSpvcontent());
        // 向各阶段审批操作过程表插入数据
        boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
        if (!success) {
            throw new HWSystemException();
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }




    //查询站点和县区信息
    public Map<String, Object> querysqStation(CaseMainCityVO vo) {

        //为反馈单位提供站点名和所属县中心名
        EntityWrapper<HwBaseCountycore> countryWrapper = new EntityWrapper<>();
        countryWrapper.eq("F_AreaId",vo.getfAreacode());
        List<HwBaseCountycore> hwBaseCountycore = hwBaseCountycoreMapper.selectList(countryWrapper);


        CaseMainCityVO resultVo = new CaseMainCityVO();
        if("".equals(vo.getfAjCrtstep())&&"".equals(vo.getFlowCode())){
            resultVo.setDefautCode(vo.getfOverruncode());
        } else {
            HwBaseWorkflow hwBaseWorkflow = baseWorkflowService.getPresentWorkflow(vo.getfAjCrtstep(), vo.getFlowCode());
            if ("2".equals(hwBaseWorkflow.getWorkflowDepcodetype())) {
                resultVo.setDefautCode(hwBaseCountycore.get(0).getfCountycorecode());
            } else {
                resultVo.setDefautCode(vo.getfOverruncode());
            }
        }

        resultVo.setCountryType("2");
        resultVo.setStationType("3");
        resultVo.setStationCode(vo.getfOverruncode());
        resultVo.setStationName(vo.getfOverrunname());
        resultVo.setCountryCode(hwBaseCountycore.get(0).getfCountycorecode());
        resultVo.setCountryName(hwBaseCountycore.get(0).getfCountycorename());

        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, resultVo);
    }
}
